<template>
  <div class="from-container">
    <AppSearchForm @submit="getList()" @reset="reset()">
      <el-row>
        <el-col :span="6">
          <el-form-item :label="cashType == 'income' ? '收款单号' : '付款单号'">
            <el-input
              v-model="query.DocNo"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="发票号">
            <el-input
              v-model="query.InvoiceNo"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="提单号">
            <el-input
              v-model="query.BlNo"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="业务员">
            <el-select
              v-model="query.SaleId"
              placeholder="请选择业务员"
              style="width: 100%"
              clearable
              filterable
              remote
              :remote-method="getSalesman"
              :loading="salesmanLoading"
            >
              <el-option
                v-for="item in salesman"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item :label="cashType == 'income' ? '收款日期' : '付款日期'">
            <el-date-picker
              v-model="payTimeRange"
              type="daterange"
              range-separator="~"
              start-placeholder="开始时间"
              end-placeholder="结束时间"
              style="width: 100%"
              size="small"
              value-format="YYYY-MM-DD"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="金额">
            <div class="flex-layout">
              <el-input
                v-model="query.SmallAmount"
                clearable
                placeholder="请输入起始金额"
                style="width: 50%"
                @input="
                  query.SmallAmount = validateOnlyNumber(query.SmallAmount)
                "
                @blur="loseFocusMoney"
              />
              <el-input
                v-model="query.BigAmout"
                clearable
                placeholder="请输入截止金额"
                style="width: 50%; margin-left: 4px"
                @input="query.BigAmout = validateOnlyNumber(query.BigAmout)"
                @blur="loseFocusMoney"
              />
            </div>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <!-- <el-form-item label="结算对象">
            <el-input
              v-model="query.EntId"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item> -->
          <el-form-item label="结算对象">
            <el-select
              v-model="query.EntId"
              placeholder="请选择结算对象"
              style="width: 100%"
              clearable
              filterable
              remote
              :remote-method="getSaleMan"
              :loading="SaleManLoading"
            >
              <el-option
                v-for="item in SaleMan"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right">{{ item.entFullname }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="核销状态">
            <el-select
              v-model="query.FeeStatus"
              placeholder="请选择核销状态"
              style="width: 100%"
            >
              <el-option
                v-for="item in WriteOffStatus"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="对账单号">
            <el-input
              v-model="query.BalanceCode"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="审核状态">
            <el-select
              v-model="query.CheckStatus"
              placeholder="请选择审核状态"
              style="width: 100%"
            >
              <el-option
                v-for="(item, index) in aduitStatus"
                :key="index"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="币种">
            <el-select
              v-model="query.Currency"
              placeholder="请选择币种"
              style="width: 100%"
              clearable
              filterable
              remote
              :remote-method="getCurrentData"
              :loading="currentLoading"
            >
              <el-option
                v-for="item in current"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              >
                <!-- <span style="float: left"> {{ item.value }}|{{ item.label }}</span> -->
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="备注">
            <el-input
              v-model="query.Memo"
              clearable
              placeholder="请输入"
              style="width: 100%"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </AppSearchForm>
  </div>
</template>

<script setup>
import { ref, computed, defineProps, defineEmits } from 'vue'
import AppSearchForm from '@/components/AppSearchForm.vue'
import request from '@/public/request'
import { validateOnlyNumber } from '@/utils/validate.js'

// 定义 props
const props = defineProps({
  query: {
    type: Object,
    default: () => ({})
  },
  cashType: {
    type: String,
    default: 'income'
  }
})
// 定义 emits
const emit = defineEmits(['reset', 'getList'])

// 定义响应式数据
const salesman = ref([])
const salesmanLoading = ref(false)

const WriteOffStatus = [
  { label: '全部', value: -2 },
  { label: '未销', value: 0 },
  { label: '已销', value: 1 }
]

const aduitStatus = [
  { label: '全部', value: -2 },
  { label: '未审核', value: 0 },
  { label: '已审核', value: 1 }
]
// 币种
const current = ref([])
const currentLoading = ref(false)

// 计算属性
const payTimeRange = computed({
  get() {
    return [props.query.PayStartTime, props.query.PayEndTime]
  },
  set(val) {
    if (val) {
      props.query.PayStartTime = val[0]
      props.query.PayEndTime = val[1]
    } else {
      props.query.PayStartTime = ''
      props.query.PayEndTime = ''
    }
  }
})
// 获取币种类型
const getCurrentData = async (val) => {
  currentLoading.value = true
  let res = await request.get('/api/base/currency/dropdownlist', {
    keywords: val
  })
  current.value = res.map((item) => {
    return {
      label: item.currencyCode + '|' + item.currencyName,
      value: item.currencyCode
    }
  })
  currentLoading.value = false
}
//终止金额始终比起始金额多
const loseFocusMoney = () => {
  let LessMoney = parseFloat(props.query.SmallAmount)
  let MoreMoney = parseFloat(props.query.BigAmout)
  if (LessMoney > 0 && MoreMoney > 0 && LessMoney > MoreMoney) {
    props.query.BigAmout = (LessMoney + 1).toString()
  }
}
// 获取业务员
const getSalesman = async (val) => {
  salesmanLoading.value = true
  let res = await request.get('/api/user/userList', {
    keyword: val
  })
  res.forEach((item) => {
    item.label = item.nickName
    item.value = item.id
  })
  salesman.value = res
  salesmanLoading.value = false
}

let SaleMan = ref([])
let SaleManLoading = ref(false)
const getSaleMan = async (val) => {
  SaleManLoading.value = true
  let res = await request.get('/api/buenterprise/dropdownlist', {
    KeyWords: val
  })
  res.forEach((item) => {
    item.label = item.entCode
    item.value = item.id
  })
  SaleMan.value = res
  SaleManLoading.value = false
}
// 重置
const reset = () => {
  emit('reset')
}
const getList = () => {
  emit('getList')
}
</script>

<style lang="scss" scoped>
.from-container {
  border: 1px solid #e8e8e8;
  padding: 6px 6px 6px 8px;
  background-color: #ffffff;
}

.flex-layout {
  display: flex;
  align-items: centers;
}

:deep(.el-form-item--small .el-form-item__label) {
  font-size: 12px;
  color: rgba(0, 0, 0, 0.85);
}

:deep(.el-form--inline .el-form-item) {
  display: flex;
  margin-right: 0;
}

:deep(.el-form-item--small.el-form-item) {
  margin-bottom: 8px;
}

:deep(.el-form--inline .el-form-item) {
  display: flex;
}
</style>
